package com.doget.data.base;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseActivity extends AppCompatActivity implements IBaseViewer {


    protected Fragment mCurrentReplaceFragment;
    protected CompositeDisposable mCompositeDisposable;
//    protected Unbinder mUnbinder;

    private void setAndroidNativeLightStatusBar(boolean dark) {
        View decor = getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }

    }

    private void onCommonCreateView(Bundle savedInstanceState) {
        onCreateView(savedInstanceState);

        initHeader();
        initView();

        try {
            initData(savedInstanceState);
        } catch (Exception e) {

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        onCommonCreateView(savedInstanceState);
//        AppManager.getAppManager().addActivity(this);
    }

    @Override
    protected void onDestroy() {
        if (mCompositeDisposable == null) {
            mCompositeDisposable.clear();
        }
//        if (null != mUnbinder) {
//            mUnbinder.unbind();
//        }
        super.onDestroy();
    }

    public void addDisposable(Disposable disposable) {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.add(disposable);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        onCommonCreateView(savedInstanceState);
    }

    public void loadFragment(Fragment fragment, int layoutId, Bundle bundle) {
        replaceFragment(fragment, layoutId, bundle);
    }

    public void loadFragment(Fragment fragment, int layoutId) {
        replaceFragment(fragment, layoutId, null);
    }

    private void replaceFragment(Fragment fragment, int LayoutId, Bundle bundle) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //添加fragment到activity中
        mCurrentReplaceFragment = fragment;
//        ((BaseFragment) mCurrentReplaceFragment).setSupportArguments(bundle);
        transaction.replace(LayoutId, mCurrentReplaceFragment).commitAllowingStateLoss();
    }

    @Override
    public void showLoading(String message) {
//        CustomProgressDialog.showLoading(this, message);
    }

    @Override
    public void dimissLoading() {
//        CustomProgressDialog.stopLoading();
    }

    protected abstract void onCreateView(Bundle bundle);

    protected abstract void initView();

    protected abstract void initData(Bundle bundle);

    protected abstract void initHeader();
}
